package com.spzx.channel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ItemVo;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.SkuPrice;
import com.spzx.product.api.domain.vo.SkuStockVo;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
* 商品详情Service
*/
@Service
public class ItemServiceImpl implements IItemService {
    @Autowired
    RemoteProductService remoteProductService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public ItemVo item(Long skuId) {

        //远程调用商品微服务接口之前 提前知道用户访问商品SKUId是否存在与布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("sku:bloom:filter");
        if (!bloomFilter.contains(skuId)){
            throw new ServiceException("用户查询商品sku不存在");
        }

        ItemVo itemVo = new ItemVo();

        //1.根据skuId查询productSku
        CompletableFuture<ProductSku> SkuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
            if (R.FAIL == productSkuResult.getCode()) {
                throw new ServiceException(productSkuResult.getMsg());
            }
            ProductSku productSku = productSkuResult.getData();
            itemVo.setProductSku(productSku);
            return productSku;
        }, threadPoolExecutor);


        //2.根据productId查询product
        CompletableFuture<Void> productCompletableFuture = SkuCompletableFuture.thenAcceptAsync(productSku -> {
            R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productResult.getCode()) {
                throw new ServiceException(productResult.getMsg());
            }
            Product product = productResult.getData();
            itemVo.setProduct(product);
            //2.1查询轮播图
            String sliderUrls = product.getSliderUrls();
            String[] sliderUrlsArray = sliderUrls.split(",");
            itemVo.setSliderUrlList(Arrays.asList(sliderUrlsArray));
            //2.2查询商品规格信息
            String specValue = product.getSpecValue();
            JSONArray specValueJson = JSON.parseArray(specValue);
            itemVo.setSpecValueList(specValueJson);
        }, threadPoolExecutor);

        //3.根据skuId查询skuPrice
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuPriceResult.getCode()) {
                throw new ServiceException(skuPriceResult.getMsg());
            }
            SkuPrice skuPrice = skuPriceResult.getData();
            itemVo.setSkuPrice(skuPrice);
        }, threadPoolExecutor);


        //4.根据productId查询detailsImageUrlList
        CompletableFuture<Void> productDetailsCompletableFuture = SkuCompletableFuture.thenAcceptAsync(productSku -> {
            R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productDetailsResult.getCode()) {
                throw new ServiceException(productDetailsResult.getMsg());
            }
            ProductDetails productDetails = productDetailsResult.getData();
            String imageUrls = productDetails.getImageUrls();
            String[] imageUrlsArray = imageUrls.split(",");
            itemVo.setDetailsImageUrlList(Arrays.asList(imageUrlsArray));
        }, threadPoolExecutor);


        //5.根据skuId查询skuStockVo
        CompletableFuture<Void> skuStockCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuStockResult.getCode()) {
                throw new ServiceException(skuStockResult.getMsg());
            }
            SkuStockVo skuStockVo = skuStockResult.getData();
            itemVo.setSkuStockVo(skuStockVo);
        }, threadPoolExecutor);


        //6.根据productId查询sku列表的数据,得到Map<String,Long>
        CompletableFuture<Void> skuSpecValueCompletableFuture = SkuCompletableFuture.thenAcceptAsync(productSku -> {
            R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == skuSpecValueResult.getCode()) {
                throw new ServiceException(skuSpecValueResult.getMsg());
            }
            Map<String, Long> skuSpecValueResultData = skuSpecValueResult.getData();
            itemVo.setSkuSpecValueMap(skuSpecValueResultData);
        }, threadPoolExecutor);

        //组合以上七个异步任务
        CompletableFuture.allOf(
                SkuCompletableFuture,
                productCompletableFuture,
                skuPriceCompletableFuture,
                productDetailsCompletableFuture,
                skuStockCompletableFuture,
                skuSpecValueCompletableFuture
        ).join();


        return itemVo;
    }
}
